/**
 * 炮塔
 * Created by zhd on 2017/6/23.
 */
var TurretSprite = cc.Sprite.extend({
    _bullets: [],// 当前填充的子弹
    _isRadiated: true, // 子弹能否发射

    ctor: function () {
        this._super();
        this.loadInit();

    },

    loadInit: function () {
        this.loadTurret();
        this.loadTouchListener();

        this.fillBullet();
        this.fillBullet();
    },

    /**
     * 初始化炮台
     */
    loadTurret: function () {
        this.setSpriteFrame('turret1Bg.png');
        this.setPosition(g_levelInfo.turret_pos.x, g_levelInfo.turret_pos.y);
        var turretSprite = new cc.Sprite();
        turretSprite.setSpriteFrame('turret1.png');
        turretSprite.setName('turret');
        turretSprite.setPosition(this.width / 2, this.height / 2);
        this.addChild(turretSprite, 1);
    },

    /**
     * 子弹填充
     */
    fillBullet: function (index) {
        if (index === undefined) {
            index = parseInt(Math.random() * GC.turrets.length, 10);
        }

        var bulletSprite = new BulletSprite(index);
        this.addChild(bulletSprite, 0);
        this._bullets.push(bulletSprite);

        for (var i = 0; i < this._bullets.length; i++) {
            if (!i) {
                this._bullets[i].setPosition(this.width / 2, this.height - bulletSprite.height / 3);
            } else {
                this._bullets[i].setPosition(this.width / 2, this.height / 2);
            }
        }
    },

    /**
     * 返回并移除炮口的子弹
     * @returns {*}
     */
    getClearBullet: function () {
        var bullet = this._bullets.shift();
        this.removeChild(bullet);
        return bullet;
    },

    /**
     * 加载点击事件
     */
    loadTouchListener: function () {
        this.m_listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            target: this,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved: this.onTouchMoved,
            onTouchEnded: this.onTouchEnded
        });
        cc.eventManager.addListener(this.m_listener, this);
    },

    /**
     * 触摸开始
     * @param touch
     * @param event
     * @returns {boolean}
     */
    onTouchBegan: function (touch, event) {
        // 设置炮台的位置
        var self = this.target;
        var touchPos = touch.getLocation();
        var turretPos = self.getPosition();
        var angle = 180 / Math.PI * Math.atan2(touchPos.x - turretPos.x, touchPos.y - turretPos.y);
        self.runAction(cc.rotateTo(0.02, angle));

        return true;
    },

    /**
     * 触摸移动
     * @param touch
     * @param event
     */
    onTouchMoved: function (touch, event) {
        // 设置炮台的位置
        var self = this.target;
        var touchPos = touch.getLocation();
        var turretPos = self.getPosition();
        var angle = 180 / Math.PI * Math.atan2(touchPos.x - turretPos.x, touchPos.y - turretPos.y);
        self.rotation = angle;
    },

    /**
     * 触摸结束
     * @param touch
     * @param event
     */
    onTouchEnded: function (touch, event) {
        // 子弹发射
        var self = this.target;

        if (gameLayer._turretSprite._isRadiated) {
            gameLayer._turretSprite._isRadiated = false;
            
            self.scheduleOnce(function () {
                var rotation = self.rotation < 0 ? 360 + self.rotation : self.rotation;
                var radian = rotation * (Math.PI / 180);
                // 原点
                var origin = {x: this.x, y: this.y};

                // 发射的子弹
                var bullet = self.getClearBullet();
                // 炮台口到炮台的半径
                var r = this.height / 2 - bullet.height / 3;

                // 子弹在屏幕初始位置，采用三角函数运算
                var bullet_x = origin.x + r * Math.sin(radian);
                var bullet_y = origin.y + r * Math.cos(radian);
                bullet.setPosition(cc.p(bullet_x, bullet_y));

                // 设置动画
                bullet.loadAnimation();

                // 发射角度设置
                bullet.rotation = rotation;

                // 发射子弹
                bullet.radiate();

                // 将子弹移出炮台，准备发射
                gameLayer.addChild(bullet, 2);

                // 填充子弹
                self.fillBullet();
            }, 0.01);
        }
    }
});